<?php
namespace Quiz\Controller;


use Think\Controller;


/**
 * 后台首页控制器
 */
class MbtiController extends Controller{

    /**
     * 显示问卷
     * @param $user_id
     */

    public function show_mbti($user_id)
    {

        $data = M("calc_result")->where(["quiz_id" => 12, "user_id" => $user_id])->select();
        $desc = "";
        foreach($data as $value)
        {
            if ($value['divisor_level'] == 2){

                $desc = M("element_desc")->where(['quiz_id'=>12, 'element_index'=>$value['row_score']])->find();
                break;
            }

            if ($value["element_index"] == 1){
                $low_s = 11;
                $high_s = 21;
               if ($value["trans_score"] == 0){
                   $score['EI'] = [
                       "type" => "i",
                       "percent_I" =>round(( $value['row_score'] ) / ($high_s ), 4)*100,
                       "row_percent_E" => round(($value['row_score'] /  $high_s), 4)*100 ,
                       "row_percent_I" => round(($high_s - $value['row_score']) / $high_s, 4)*100
                   ];
               }
                else{
                    $score['EI'] = [
                        "type" => "E",
                        "percent_E" =>round(( $high_s - $value['row_score'] ) /($high_s), 4)*100,
                        "row_percent_E" =>round(( $high_s - $value['row_score']) / $high_s, 4)*100,
                        "row_percent_I" =>round($value['row_score'] /$high_s, 4)*100//1是减后的分
                    ];
                }
            }

            else if ($value["element_index"] == 2){
                $low_s = 13;
                $high_s = 26;
                if ($value["trans_score"] == 0){
                    $score['SN'] = [
                        "type" => "N",
                        "percent_N" =>round(( $value['row_score']  ) / ($high_s ), 4)*100,
                        "row_percent_N" =>round( $value['row_score'] /  $high_s, 4)*100,
                        "row_percent_S" =>round(($high_s - $value['row_score']) / $high_s, 4)*100
                    ];
                }
                else{
                    $score['SN'] = [
                        "type" => "S",
                        "percent_S" =>round(($high_s - $value['row_score']) /($high_s ), 4)*100,
                        "row_percent_N" =>round(( $high_s - $value['row_score']) / $high_s, 4)*100,
                        "row_percent_S" =>round($value['row_score'] /$high_s ,4)*100
                    ];
                }
            }


            else if ($value["element_index"] == 3){
                $low_s = 12;
                $high_s = 24;
                if ($value["trans_score"] == 0){
                    $score['TF'] = [
                        "type" => "F",
                        "percent_F" =>round(( $value['row_score'] ) / ($high_s) ,4 )*100,
                        "row_percent_T" =>round( $value['row_score'] /  $high_s ,4 )*100 ,
                        "row_percent_F" =>round(($high_s - $value['row_score']) /$high_s,4 )*100
                    ];
                }
                else{
                    $score['TF'] = [
                        "type" => "T",
                        "percent_T" =>round( ( $high_s - $value['row_score']  ) /($high_s),4 )*100,
                        "row_percent_T" =>round(( $high_s - $value['row_score']) / $high_s ,4)*100,
                        "row_percent_F" =>round($value['row_score'] /$high_s ,4)*100
                    ];
                }
            }

            else if ($value["element_index"] == 4){
                $low_s = 11;
                $high_s = 22;
                if ($value["trans_score"] == 0){
                    $score['JP'] = [
                        "type" => "P",
                        "percent_P" =>round((  $value['row_score'] ) / ($high_s ),4)*100,
                        "row_percent_P" => round($value['row_score'] /  $high_s,4) *100,
                        "row_percent_J" =>round(($high_s - $value['row_score']) / $high_s,4)*100
                    ];
                }
                else{
                    $score['JP'] = [
                        "type" => "J",
                        "percent_J" =>round(($high_s-  $value['row_score'] ) /($high_s ),4)*100,
                        "row_percent_P" =>round(( $high_s - $value['row_score']) / $high_s,4)*100,
                        "row_percent_J" =>round($value['row_score'] /$high_s,4)*100
                    ];
                }
            }


        }

       // $desc = M("element_desc")->where([''])


      //var_dump($score);
        $user_info = M("users")->where(['uid'=>$user_id])->find();
        $this->assign("userinfo", $user_info);
       $this->assign("score", $score);
        $this->assign("desc", $desc);
       $this->display("Quiz@Mbti:show_mbti");
    }


    /**
     * 显示团体报告
     * @param int $oid
     * @param string $quiztitle
     * @param int $display
     * @return array
     */


    public function show_mbti_groupreport($oid =0, $quiz_grouptitle = "")
    {
        $show_warn = 1;
        if (!$oid && !$quiz_grouptitle){
            $uid_list = M()->query("select users.* from users, calc_result where users.uid = calc_result.user_id and users.oid = 0 and calc_result.quiz_id=12 GROUP BY users.uid;");
            $show_warn = 0;
        }
        else{
            // $uid_list = M('orgnizer_userquizlist')->field('uid')->where(['quiztitle' => $quiz_grouptitle , 'oid' => $oid])->select();
            //$uid_list = M('orgnizer_userquizlist')->field('uid')->where(['testgroup_id' => 8 , 'oid' => 8])->select();
            $uid_list = M('orgnizer_userquizlist')->field('uid')->where(['title' => $quiz_grouptitle , 'oid' => $oid])->select();
        }

        $tvalue_list = M('calc_result');
        $rs = [];
        $i = 0;

        foreach($uid_list as $uid){
            $data = $tvalue_list->where(['user_id' => $uid['uid'], 'quiz_id' => 12])->select();
            if (!$oid && !$quiz_grouptitle){

            }
            else{
                $userinfo = D('Orgnizeruserclass')->relation(true)->where(['uid' => $uid['uid']])->select();
            }


            $userinfo = $userinfo[0];



            if ($data){
                $rs[$i]['score_data'] = $data;
                if (!$oid && !$quiz_grouptitle){
                    $rs[$i]['username'] =$uid['username'];
                }
                else{
                    $rs[$i]['username'] =$userinfo['Users']['username'];
                }

                $rs[$i]['class'] = convertClassJson($userinfo['class']);
                $rs[$i]['age'] = $userinfo['Users']['age'];


                //$rs[$i]['sex'] = $userinfo['Users']['sex'];

                if($userinfo['Users']['sex'] == 'male')
                    $rs[$i]['sex'] = '男';
                else
                    $rs[$i]['sex'] = '女';

                $i ++;
            }
        }

        $i = 0;
        $element_info = M("element_info")->where(["quiz_id" => 12])->select();
        $element_name = [];
        foreach($element_info as $e){
            $element_name[$i] = $e['element_name'].$e['element_label'];
            $i++;
        }

        $result_table = A("Quiz/Quiz")->WarnUserList($rs,$oid,"mbti", $quiz_grouptitle);

        $this->assign("element_name", $element_name);
        $this->assign('result_table', $result_table);
        $this->assign('title', $quiz_grouptitle);
        $this->assign('show_warn', $show_warn);
        $this->display("Quiz@Mbti:show_mbti_groupreport");


    }


    /**
     * 计算问卷
     * @param $uid
     * @throws \Exception
     */

    public function calcAnswer($uid)
    {
        $url = "http://localhost:8090/demo/Main";

        $data['action'] = "calc";
        $data['userid'] = $uid;
        $data['quiz_id'] = 12;
        $data['sex'] = '男';

        $httpstr = http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));//调用计算部分java

        if ($httpstr)
            return true;
        else
            return false;

        //var_dump($httpstr);
    }


    /**
     * 测试随机生成答案
     * @param $user_id
     */

    public function randomAnswer($user_id)
    {
        for($qst_id = 1; $qst_id <= 93; $qst_id++){

            $rs[] = [
              'quiz_id' => 12,
              'qst_id' => $qst_id,
                'selection' => rand(1,2),
                'user_id' => $user_id

            ];

        }


        //var_dump($rs);
        M('user_answer')->addAll($rs);
        if ($this->calcAnswer($user_id))
            echo "$user_id mbti random answer success";
        else
            echo "$user_id mbti random answer failed";

    }

    public function mbti_sample()
    {
        $this->show_mbti(34);
    }

}
